package bot.send.msssgin;

import org.springframework.web.bind.annotation.RequestParam;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class BossQQEmailSend {
    public static void sendEmail(String sendQQ, String Text) throws AddressException, MessagingException, IOException, InterruptedException {
        Properties prop = new Properties();//配置文件存邮件信息
        prop.load(new InputStreamReader(Sent.class.getClassLoader().getResourceAsStream("application.properties"), "UTF-8"));
        String auth_code = prop.getProperty("mail.auth_code");
        String subject = prop.getProperty("mail.subject");
        String sender = prop.getProperty("mail.sender");
        String addressee = sendQQ;
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");// 连接协议
        properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
        properties.put("mail.smtp.port", 465);// 端口号
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
        properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
        properties.put("mail.smtp.ssl.protocols", "TLSv1.2");//重要
        // 得到回话对象
        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        // 设置发件人邮箱地址
        message.setFrom(new InternetAddress(sender));
        // 设置收件人邮箱地址
        //String email = "";
        message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(addressee)});
        // 设置邮件标题
        message.setSubject(subject);

        // 设置邮件内容
        //一个Multipart对象包含一个或多个bodypart对象，组成邮件正文
        MimeMultipart multipart = new MimeMultipart();
        //将文本和图片添加到multipart
        //创建文本节点
        MimeBodyPart text = new MimeBodyPart();
        text.setContent(Text,"text/html;charset=UTF-8");
        multipart.addBodyPart(text);//纯文本

        multipart.setSubType("mixed");//混合关系
        message.setContent(multipart);
        // 得到邮差对象
        Transport transport = session.getTransport();
        // 连接自己的邮箱账户
        transport.connect(sender, auth_code);// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码,输入自己的即可

        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    public static void main(String[] args) throws MessagingException, IOException, InterruptedException {
        BossQQEmailSend.sendEmail("2595931767@qq.com","this\r\nText");
    }
}
